<?php

// truncate table producto;

function conectarMySql($schema = 'cdcol', $usu = 'root', $pwd = '', $host = 'localhost') {
	try {
		$dsn = "mysql:host=$host;dbname=$schema";
		$db = new PDO ( $dsn, $usu, $pwd );
		return $db;
	} catch ( PDOException $e ) {
		print ('Error de conexión') ;
		die ();
	}
}

function getColunns($database, $table) {
	$bbdd = conectarMySql ( 'information_schema', 'admin', 'admin', 'localhost' );
	
	$cons = "select column_name from columns where table_schema = '$database' and table_name='$table'";
	$res = $bbdd->query ( $cons );
	
	if (! $res) {
		return null;
	} else {
		$sol = array ();
		foreach ( $res as $fila ) {
			$i = 0;
			foreach ( $fila as $indice => $valor ) {
				if ($i % 2 == 0) {
					array_push ( $sol, $valor );
				}
				$i ++;
			}
		}
		return $sol;
	}
}

function generarTabla($database, $table) {
	$colum = getColunns ( $database, $table );
	
	$bbdd = conectarMySql ( $database, 'admin', 'admin', 'localhost' );
	
	$consulta = "select * from $table";
	$resultado = $bbdd->query ( $consulta );
		
	$tabla = "";
	
	$titulo = "<tr class='thead'>\r\n";
	foreach ( $colum as $dato ) {
		$titulo .= "<th>$dato</th>\r\n";
	}
	$titulo .= "</tr>\r\n";
	
	
	foreach ( $resultado as $fila ) {
		$i = 0;
		$tabla .= "<tr>\r\n";
		foreach ( $fila as $valores ) {
			if ($i % 2 == 0) {
				$tabla .= "<td>$valores</td>\r\n";
			}
			$i ++;
		}
		$tabla .= "</tr>\r\n";
	}
	
	return "<table> $titulo $tabla </table>";
}



?>